package controllers.resource;

import java.util.Date;

import framework.base.BaseController;
import framework.base.PagerRS;
import framework.utils.SecurityUtil;
import models.resource.TrunkNo;
import models.resource.vo.TrunkNoQuery;
import models.security.Role;
import models.security.User;
import models.security.vo.UserQuery;
import play.data.binding.ParamNode;
import play.data.validation.Valid;
/**
 * 中继号码资源
 * @author jinchaoyang
 */
public class TrunkNos extends BaseController{
    /**
     * 首页
     * @param page
     * @param query
     */
    public static void index(Integer page,TrunkNoQuery query){
        PagerRS<TrunkNo> rs = (PagerRS<TrunkNo>) TrunkNo.findByPager(getQuery(query, TrunkNoQuery.class), getPageNo(page),
                getPageSize());
        render(rs);
    }
    /**
     * 新增
     */
    public static void add(){
        render();
    }
    /**
     * 创建
     * @param trunkNo
     */
    public static void create(@Valid TrunkNo trunkNo){
        if(validation.hasErrors()){
            saveUserLogAndNotice("中继号创建失败", false);
            render("@add");
        }else{
            String loginerId = SecurityUtil.getLoginUserId();
            trunkNo.creatorId = loginerId;
            trunkNo.operatorId = loginerId;
            trunkNo.save();
            saveUserLogAndNotice("中继号创建成功", true);
            index(null,null);
        }
    }
    /**
     * 编辑
     * @param id
     */
    public static void edit(String id){
       TrunkNo trunkNo = TrunkNo.findById(id);
       render(trunkNo);
    }
    
    /**
     * 更新
     * @param id
     */
    public static void update(String id){
        TrunkNo trunkNo = TrunkNo.findById(id);
        trunkNo.edit(ParamNode.convert(params.all()), "trunkNo");
        validation.valid(trunkNo);
        if(validation.hasErrors()){
            saveUserLogAndNotice("中继号更新失败", false);
            render("@edit",trunkNo);
        }else{
            trunkNo.updatedAt = new Date();
            String loginerId = SecurityUtil.getLoginUserId();
            trunkNo.operatorId = loginerId;
            trunkNo.save();
            saveUserLogAndNotice("中继号更新成功", true);
            index(null,null);
        }
    }
    
    /**
     * 删除
     * @param id
     */
    public static void destroy(String id){
        TrunkNo trunkNo = TrunkNo.findById(id);
        if(null!=trunkNo){
            String loginerId = SecurityUtil.getLoginUserId();
            trunkNo.operatorId = loginerId;
            trunkNo.destroy();
            saveUserLogAndNotice("删除成功", true);
        }else{
            saveUserLogAndNotice("删除失败", true);
        }
        index(null,null);
    }
    
    /**
     * 查看
     * @param id
     */
    public static void show(String id){
        TrunkNo trunkNo = TrunkNo.findById(id);
        render(trunkNo);
    }
    
    
    
}
